#include "basic.h"
#include <QLabel>
#include <QVBoxLayout>
#include <DGuiApplicationHelper>
#include <DStyle>
DWIDGET_USE_NAMESPACE
BasicSettings::BasicSettings(QWidget *parent) : QScrollArea(parent)
{
    setLineWidth(0);
    layout=new QVBoxLayout;
    layout->setMargin(0);
    QFont h1_font;
    h1_font.setPointSize(15);
    setLayout(layout);
    QLabel* title=new QLabel;
    title->setFont(h1_font);
    title->setText(tr("Basic Setting"));
    layout->addWidget(title);
    initMainPage();
    initSearch();
    layout->addStretch();
    loadConfig();
}

void BasicSettings::initMainPage()
{
    DFrame* mainpage_frame=new DFrame;
    mainpage_frame->setLineWidth(0);
    QVBoxLayout* mainpage_layout=new QVBoxLayout;
    QLabel* mainpage_title=new QLabel;
    mainpage_layout->addWidget(mainpage_title);
    mainpage_title->setText(tr("Main Page"));
    mainpage_frame->setLayout(mainpage_layout);
    mainpage_search=new DRadioButton;
    mainpage_address=new DRadioButton;
    mainpage_search->setText(tr("Search page"));
    mainpage_address->setText(tr("Web page"));
    mainpage_layout->addWidget(mainpage_search);
    mainpage_layout->addWidget(mainpage_address);
    mainpage_address->setChecked(true);
    mainpage_ln=new DLineEdit;
    mainpage_layout->addWidget(mainpage_ln);
    mainpage_ln->setPlaceholderText(tr("Please enter the web address here"));

    connect(mainpage_search,&DRadioButton::clicked,[=](){
        Settings::setValue("MainPage","search");
        mainpage_ln->hide();
    });
    connect(mainpage_address,&DRadioButton::clicked,[=](){
        Settings::setValue("MainPage","address");
        mainpage_ln->show();
    });
    connect(mainpage_ln,&DLineEdit::editingFinished,[=](){
        Settings::setValue("MainPageAddress",mainpage_ln->text());
    });
    layout->addWidget(mainpage_frame);

    mainpage_search->setEnabled(false);
}

void BasicSettings::initSearch()
{
    DFrame* search_frame=new DFrame;
    search_list=new DListView;
    search_frame->setLineWidth(0);
    QVBoxLayout* search_layout=new QVBoxLayout;
    search_frame->setLayout(search_layout);
    QLabel* search_title=new QLabel;
    search_title->setText(tr("Search engine"));
    search_layout->addWidget(search_title);
    search_layout->addWidget(search_list);

    QStandardItemModel *browserModel = new QStandardItemModel;
    DStandardItem *browserItem2 = new DStandardItem(QIcon::fromTheme("firefox"), "火狐浏览器");
    auto action2 = new DViewItemAction(Qt::AlignVCenter, QSize(), QSize(), true);
    auto icon2 = qobject_cast<DStyle *>(style())->standardIcon(DStyle::SP_IndicatorChecked);
    action2->setIcon(icon2);
    browserItem2->setActionList(Qt::Edge::RightEdge, {action2});
    DStandardItem *browserItem1 = new DStandardItem(QIcon::fromTheme("chrome"), "谷歌浏览器");
    auto action = new DViewItemAction(Qt::AlignVCenter, QSize(), QSize(), true);
    auto icon = qobject_cast<DStyle *>(style())->standardIcon(DStyle::SP_IndicatorChecked);
    action->setIcon(icon);
    browserItem1->setActionList(Qt::Edge::RightEdge, {action});

    browserItem2->actionList(Qt::Edge::RightEdge);
    browserItem2->setEditable(false);
//    browserItem2->setCheckable(true);
    browserItem1->setEditable(false);
//    browserItem1->setCheckable(true);
    browserModel->appendRow(browserItem2);
    browserModel->appendRow(browserItem1);
    search_list->setModel(browserModel);

    search_list->setLineWidth(0);

    layout->addWidget(search_frame);

}

void BasicSettings::loadConfig()
{
    //主页选项
    QString mainpage=Settings::getValue("MainPage");
    if(mainpage=="search"){
        //搜索页
        emit mainpage_search->clicked();
        mainpage_search->setChecked(true);
    }
    else if(mainpage=="address"){
        //定义网址
        emit mainpage_address->clicked();
        mainpage_address->setChecked(true);
    }else {
        //默认
        emit mainpage_address->clicked();
        mainpage_address->setChecked(true);
    }
    mainpage_ln->setText(Settings::getValue("MainPageAddress"));
}
